package com.xcdh.learn.util;

import com.xcdh.learn.entity.ContentEx;

import java.util.ArrayList;
import java.util.List;

public class TreeUtil {


    public static List<ContentEx> buildTree(List<ContentEx> list){
        List<ContentEx> parent = new ArrayList<>() ;
        for (ContentEx contentEx : list) {
            if(contentEx.getParentId().compareTo(0L) == 0){
                findChild(contentEx , list) ;
                parent.add(contentEx) ;
            }

        }
        return parent ;
    }

    private static void findChild(ContentEx parent , List<ContentEx> list){
        for (ContentEx contentEx : list) {
            if(contentEx.getParentId().compareTo(parent.getId()) == 0){
                parent.getChildren().add(contentEx) ;
                findChild(contentEx , list) ;
            }
        }
    }
}
